package com.kele.blockqueue;

import java.util.Collection;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author kele
 * @date 2021/10/11 16:18
 * @description
 */
public class BlockQueueTest {
    public static void main(String[] args) throws InterruptedException {

        /**
         * 四组qpi
         * 1.抛出异常 test1();
         * 2.有返回值不抛出异常 test2();
         * 3.等待阻塞 一直阻塞 test3();
         * 4.阻塞设置超时时间
         */
        //test1();
        //test2();
        //test3();
        test4();


    }
    public static  void test1(){
        BlockingQueue blockingQueue=new ArrayBlockingQueue(3);
        System.out.println(blockingQueue.add("1"));
        System.out.println(blockingQueue.add("2"));
        System.out.println(blockingQueue.add("3"));
        /**
         * 检测队首元素
         */
        System.out.println(blockingQueue.element());
        /**
         * 队列已满会抛出异常
         */
        System.out.println(blockingQueue.add("3"));

        System.out.println("=====================");
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        /**
         * 队列已空会抛出异常
         */
        System.out.println(blockingQueue.remove());

    }
    public static  void test2(){
        BlockingQueue blockingQueue=new ArrayBlockingQueue(3);
        System.out.println(blockingQueue.offer("1"));
        System.out.println(blockingQueue.offer("2"));
        System.out.println(blockingQueue.offer("3"));

        /**
         * 检测队首元素
         */
        System.out.println(blockingQueue.peek());
        /**
         * 队列已满不会抛出异常 false
         */
        System.out.println(blockingQueue.offer("3"));

        System.out.println("=====================");
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        /**
         * 队列已空不会抛出异常 null
         */
        System.out.println(blockingQueue.poll());

    }
    public static  void test3() throws InterruptedException {
        BlockingQueue blockingQueue=new ArrayBlockingQueue(3);

        blockingQueue.put("1");
        blockingQueue.put("2");
        blockingQueue.put("3");

        /**
         * 检测队首元素
         */
        System.out.println(blockingQueue.peek());
        /**
         * 队列已满 一直阻塞
         */
        blockingQueue.put("4");

        System.out.println("=====================");
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        /**
         * 没有元素会一直阻塞
         */
        System.out.println(blockingQueue.take());

    }
    public static  void test4() throws InterruptedException {
        BlockingQueue blockingQueue=new ArrayBlockingQueue(3);

        blockingQueue.offer("1");
        blockingQueue.offer("2");
        blockingQueue.offer("3");
        /**
         * 队列已满 给上超时推出时间
         */
        blockingQueue.offer("4",1, TimeUnit.SECONDS);

        System.out.println("=====================");
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        /**
         * 没有元素 给上超时推出时间
         */
        System.out.println(blockingQueue.poll(1,TimeUnit.SECONDS));

    }
}
